package com.jtlrm.ckd.mvp.view.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.ImageUtil;
import com.base.sdk.util.ZXingUtils;
import com.base.sdk.widget.TitleBar;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.BaseActivity;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.UserEntity;
import com.jtlrm.ckd.mvp.model.UserModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;

import butterknife.BindView;

/**
 * 我的二维码
 */
public class MyQRCodeActivity extends TitleBarActivity {
    @BindView(R.id.avatar)
    ImageView avatarI;
    @BindView(R.id.name)
    TextView nameT;
    @BindView(R.id.qr_code)
    ImageView qrI;
    UserModel userModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return "我的二维码";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_my_qrcode);
    }

    @Override
    protected void initView() {
        UserEntity userEntity = UserHelper.getInstance(context).getUserInfo();
        if (userEntity != null) {
            setText(nameT, userEntity.getName());
            ImageUtil.loadCircleImage(avatarI, userEntity.getHeadPicture());
        }
        userModel = new UserModel();
        queryCode();
    }

    private void queryCode() {
        showLoadingDialog("加载中");
        userModel.queryQRCode(new CommonObserver<String>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(String data) {
                dismissLoadingDialog();
                try {
                    Bitmap bitmap = ZXingUtils.createQRImage(data, 400, 400);
                    ImageUtil.loadImageBitmap(qrI, bitmap);
                } catch (Exception e) {

                }
            }
        }, lifecycleSubject);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

    }
}
